package com.example.zqathserver.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/siam/oauth2.0")
public class YFJTOAuth2ServerController {

    /**
     * 授权端点 - 直接重定向回客户端并返回固定授权码
     * 请求示例: GET /siam/oauth2.0/authorize?response_type=code&client_id=test&redirect_uri=http://...
     */
    @GetMapping("/authorize")
    public ResponseEntity<String> authorizeEndpoint(
            @RequestParam("response_type") String responseType,
            @RequestParam("client_id") String clientId,
            @RequestParam("redirect_uri") String redirectUri,
            @RequestParam(value = "state", required = false) String state) {

        // 直接生成固定授权码并重定向
        String authorizationCode = "ST-7-NzUWauaeMYEAUgGBxr1q-siam";
        String redirectUrl = redirectUri + "?code=" + authorizationCode;

        if (state != null) {
            redirectUrl += "&state=" + state;
        }

        return ResponseEntity.status(HttpStatus.FOUND)
                .header("Location", redirectUrl)
                .build();
    }

    /**
     * 令牌端点 - 直接返回固定访问令牌 (标准格式)
     * 请求示例: POST /siam/oauth2.0/accessToken
     */
    @PostMapping(value = "/accessToken", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public ResponseEntity<String> accessTokenEndpointStandard() {
        // 直接返回固定令牌响应
        String responseBody = "access_token=TGT-1-aKH4aHXpq703ndctxfaGw5FRfbi0qTdqrIVz3K66OQ47K3wncm-siam&expires=7200&token_type=bearer";

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                .body(responseBody);
    }

    /**
     * 令牌端点 - 直接返回固定访问令牌 (JSON格式)
     * 请求示例: POST /siam/oauth2.0/accessTokenByJson
     */
    @PostMapping(value = "/accessTokenByJson", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map<String, Object>> accessTokenEndpointJson() {
        // 直接返回固定JSON令牌响应
        Map<String, Object> response = new HashMap<>();
        response.put("access_token", "TGT-1-aKH4aHXpq703ndctxfaGw5FRfbi0qTdqrIVz3K66OQ47K3wncm-siam");
        response.put("expires", 7200);
        response.put("token_type", "bearer");
        response.put("error", "");
        response.put("status", "true");
        response.put("code", "1");
        response.put("message", "success");

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(response);
    }

    /**
     * 用户信息端点 - 直接返回固定用户信息 (标准格式)
     * 请求示例: POST /siam/oauth2.0/profile
     */
    @PostMapping(value = "/profile", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public ResponseEntity<String> profileEndpointStandard() {
        // 直接返回固定用户信息
        String responseBody = "id=wangjiang&attributes={userid=wangjiang, name=王强}";

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                .body(responseBody);
    }

    /**
     * 用户信息端点 - 直接返回固定用户信息 (JSON格式)
     * 请求示例: POST /siam/oauth2.0/profileByJson
     */
    @PostMapping(value = "/profileByJson", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map<String, Object>> profileEndpointJson() {
        // 直接返回固定JSON用户信息
        Map<String, Object> response = new HashMap<>();
        response.put("id", "01522108366832909401");

        Map<String, String> attributes = new HashMap<>();
        attributes.put("userid", "01522108366832909401");
        attributes.put("name", "王强");

        response.put("attributes", attributes);
        response.put("error", "");
        response.put("status", "true");
        response.put("code", "0");
        response.put("message", "success");

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(response);
    }

    /**
     * 登出端点 - 直接返回登出成功
     * 请求示例: GET /siam/logout?service=...
     */
    @GetMapping("/logout")
    public ResponseEntity<String> logoutEndpoint(
            @RequestParam(value = "service", required = false) String service) {

        // 如果有service参数，重定向到指定服务
        if (service != null && !service.isEmpty()) {
            return ResponseEntity.status(HttpStatus.FOUND)
                    .header("Location", service)
                    .build();
        }

        return ResponseEntity.ok("登出成功");
    }
}